﻿using System;
using System.Collections;
using System.IO;
using Nini.Config;

namespace mst_regex
{
    internal class AppSettings
    {
        private readonly string _configFilename;

        private short _startHours;
        private short _startMinutes;
        private DayOfWeek[] _weekends;

        public AppSettings(string configFile)
        {
            _configFilename = string.IsNullOrEmpty(configFile)
                                  ? Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"zeitgeist.conf")
                                  : configFile;
        }

        public DayOfWeek[] Weekends
        {
            get { return _weekends; }
            set { _weekends = value; }
        }

        public short StartHours
        {
            get { return _startHours; }
            set { _startHours = value; }
        }

        public short StartMinutes
        {
            get { return _startMinutes; }
            set { _startMinutes = value; }
        }

        public string CsvFolder { get; set; }

        public string AbExecutable { get; set; }

        public string AbDatabase { get; set; }

        public string ProxyAddress { get; set; }

        public string ProxyPass { get; set; }

        public int ProxyPort { get; set; }

        public string ProxyUser { get; set; }

        public bool UseProxy { get; set; }

        public void LoadSettings()
        {
            if (!File.Exists(_configFilename))
                return;

            var fileConfig = new XmlConfigSource();
            fileConfig.Load(_configFilename);

            IConfig cfgSection = fileConfig.Configs[@"Zeitgeist.Application"];
            AbDatabase = cfgSection.GetString("AmiBrokerEodDatabase", string.Empty);
            AbExecutable = cfgSection.GetString("AmiBrokerLocation", string.Empty);
            CsvFolder = cfgSection.GetString("CsvExportFolder", string.Empty);

            cfgSection = fileConfig.Configs[@"Zeitgeist.Scheduler"];

            string tmp = cfgSection.GetString("SnapshotTime", "17:30");
            string[] timeSplits = tmp.Split(new[] {':'});
            short.TryParse(timeSplits[0], out _startHours);
            short.TryParse(timeSplits[1], out _startMinutes);

            tmp = cfgSection.GetString("Weekends", string.Empty);
            string[] dowSplits = tmp.Split(new[] {':'});
            var tmpList = new ArrayList();
            foreach (string dow in dowSplits)
            {
                int nDow = -1;
                if (int.TryParse(dow, out nDow))
                {
                    tmpList.Add((DayOfWeek) nDow);
                }
            }

            if (tmpList.Count > 0)
                Weekends = (DayOfWeek[]) tmpList.ToArray(typeof (DayOfWeek));

            cfgSection = fileConfig.Configs[@"Zeitgeist.Network"];
            UseProxy = cfgSection.GetBoolean("UseProxy", false);
            ProxyAddress = cfgSection.GetString("ProxyAddress", string.Empty);
            ProxyPort = cfgSection.GetInt("ProxyPort", 0);
            ProxyUser = cfgSection.GetString("ProxyUser", string.Empty);
            ProxyPass = cfgSection.GetString("ProxyPass", string.Empty);
        }

        private string _WeekendsToString()
        {
            string tmp = string.Empty;
            foreach (DayOfWeek dow in _weekends)
            {
                var n = (int) dow;
                tmp = string.IsNullOrEmpty(tmp) ? n.ToString() : string.Concat(tmp, ':', n.ToString());
            }

            return string.IsNullOrEmpty(tmp) ? "" : tmp;
        }

        public void SaveSettings()
        {
            var fileConfig = new XmlConfigSource();

            IConfig cfgSection = fileConfig.AddConfig(@"Zeitgeist.Application");
            cfgSection.Set("AmiBrokerEodDatabase", AbDatabase);
            cfgSection.Set("AmiBrokerLocation", AbExecutable);
            cfgSection.Set("CsvExportFolder", CsvFolder);

            cfgSection = fileConfig.AddConfig(@"Zeitgeist.Scheduler");
            cfgSection.Set("SnapshotTime", string.Format("{0}:{1}", _startHours, _startMinutes));
            cfgSection.Set("Weekends", _WeekendsToString());

            cfgSection = fileConfig.AddConfig(@"Zeitgeist.Network");
            cfgSection.Set("UseProxy", UseProxy);
            cfgSection.Set("ProxyAddress", ProxyAddress);
            cfgSection.Set("ProxyPort", ProxyPort);
            cfgSection.Set("ProxyUser", ProxyUser);
            cfgSection.Set("ProxyPass", ProxyPass);

            fileConfig.Save(_configFilename);
        }
    }
}